<?php

class RegistrationController extends ControllerBase
{

    public static function upload($user_id,$from,$height,$width)
    {
       return \Cloudinary\Uploader::upload($from,
        array(
           "public_id" => $user_id,
           "height" => $height,
           "width" => $width,
           "invalidate" => true
        ))['version'];

    }

	public function indexAction()
	{


	}  
    public function saveAction($form ='')
    {
        if($this->session->get('user_id') == null) die('Wrong baby wrong');
        if(!$this->request->isPost()) die('Hey what are you doing');
        // this will be the callback
        $result = array();
        $user_id = $this->session->get('user_id');
        $user_info = UserAccountTb::findFirstById($user_id);
        switch($form)
        {
            case "step_1":
                $data = $this->request->getPost('data');
                $info = array();
                parse_str($data,$info);
                $validation = new Step1Validation();

                $messages = $validation->validate($info);
                if(count($messages))
                {
                    $message = '';
                    foreach ($messages as $value) {
                        $message.=$value.'<br>';
                    }
                    $result['messages'] = $message;
                    $result['status'] = false;
                }
                else if($user_info->save($info) == false)
                {   
                    $messages = '';
                    foreach ($user_info->getMessages() as $value) {
                        $messages .= $value."<br>";
                    }
                    $result['messages'] = $messages;
                    $result['status'] = false;
                }
                else
                {
                    $this->registerSession($user_info->id,$user_info->fname,$user_info->lname,$user_info->profile_pic);
                    $result['status'] = true;
                }
            break;

            case "step_2":
                $address_info = array(
                        "user_id"=>$user_id,
                        "type"=>"home_address",
                        "long"=>$this->request->getPost('longt'),
                        "lat"=>$this->request->getPost('lat'),
                        "google_add"=>$this->request->getPost('google_add'),
                        "region"=>$this->request->getPost('region'),
                        "city"=>$this->request->getPost('city'),
                        "address"=>$this->request->getPost('address')
                    );

                $info = UserAddressTb::findFirst(array(
                        "conditions" => "user_id = ?0 and type = 'home_address'",
                        "bind" => array(0=>$address_info['user_id'])
                    ));
                if($info == false)
                {
                    // INSERT NEW
                    $info = new UserAddressTb();
                }
                if($info->save($address_info) == false)
                {
                    $result['status'] = false;
                    $messages ='Please enter a valid address';
                   /* foreach ($info->getMessages() as $value) {
                        $messages.=$value."<br>";
                    } */
                    $result['messages'] = $messages;
                }
                else
                {
                    $result['status'] = true;
                }


            break;
        } // endof switch
        echo json_encode($result);
        $this->view->disable();

    }

    public function confirmAction()
    {
		$this->view->setMainView('index');
		$this->view->setVar('page_name', 'Registration');
		$this->view->setVar('content','registration/confirm');  

        $email = $_GET['ea'];
        $activation_key = $_GET['ec'];   

		if(isset($_GET['ec']) && isset($_GET['ea']))
    	{
    		
     		// check if account is already confirmed
            $result = UserAccountTb::findFirst(array(
                        'conditions' => "email = ?0 AND activation_key = ?1",
                        "bind" => array(0=>$_GET['ec'],1=>$_GET['ea'])
                        ));

            if($result == true)
            {
                // auto log in for the user
                $this->db->begin();
                $result->last_login = CURR_DATE;
                $result->activated = true;

                if($result->save() == true)
                {
                    $this->db->commit();
                    $this->response->redirect(BASE_URI.'registration/step_1');    
                }
                else
                {
                    $this->db->rollback();
                    echo "Sorry the following errors are encountered"."<br>";
                    foreach ($result->getMessages() as $value)
                    {
                        echo $value."<br>";
                    }
                    die;        
                }
                
            }
            else
            {
                $this->response->redirect(BASE_URI);   
            }
     		
    	}
        else if(isset($_GET['ea']))
        {
            // check if email is existing
            $account = UserAccountTb::findFirstByEmail($email);
            if($account == true)
            {
                // check if already activated
                if($account->activated == 0)
                {
                    $this->view->setVar('account',$account);
                }
                else
                {
                    $this->response->redirect(BASE_URI);
                }
            }
            else
            {
                //if email address is not existing
                $this->response->redirect(BASE_URI);
            }
        }
        else
        {
            //if there is no data in get
            $this->response->redirect(BASE_URI);
        }
    	
		
    }
    public function step_1Action()
    {

       if($this->session->get('user_id') == null) $this->response->redirect(BASE_URI);

        $info = UserAccountTb::findFirstById($this->session->get('user_id'));
     //   if($info == false) $this->response->redirect(BASE_URI); 

        $this->view->setVar('info',$info);
		$this->view->setMainView('index');
		$this->view->setVar('page_name', 'Step_1');
		$this->view->setVar('content','registration/step_1');  		
    }
    public function step_2Action()
    {
        if($this->session->get('user_id') == null) $this->response->redirect(BASE_URI);
		$this->view->setMainView('index');
		$this->view->setVar('page_name', 'Step_2');
		$this->view->setVar('content','registration/step_2'); 

        $address = UserAddressTb::findFirst(array(
                "conditions"=>"user_id = ?0 and type = 'home_address'",
                "bind"=>array(0=>$this->session->get('user_id'))
            ));

        $this->view->setVar('address',$address);
        
    }
    public function step_3Action()
    {
        if($this->session->get('user_id') == null) $this->response->redirect(BASE_URI);
		$this->view->setMainView('index');
		$this->view->setVar('page_name', 'Step_3');
		$this->view->setVar('content','registration/step_3');  		
    }
    public function step_4Action()
    {
        if($this->session->get('user_id') == null) $this->response->redirect(BASE_URI);
		$this->view->setMainView('index');
		$this->view->setVar('page_name', 'Step_4');
		$this->view->setVar('content','registration/step_4');  		
    }
}

